#include <rtthread.h>
#include <rtdevice.h>
#include <pin.h>

// 引脚定义（替换为实际硬件映射）
#define LED_R_PIN    GPIO62
#define LED_G_PIN    GPIO20
#define LED_B_PIN    GPIO63

// 初始化GPIO为输出模式
void led_init(void)
{
    rt_pin_mode(LED_R_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(LED_G_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(LED_B_PIN, PIN_MODE_OUTPUT);
    
    // 初始熄灭（高电平，因硬件为“VDD_3V3→电阻→LED→GPIO”，GPIO高则LED灭）
    rt_pin_write(LED_R_PIN, PIN_HIGH);
    rt_pin_write(LED_G_PIN, PIN_HIGH);
    rt_pin_write(LED_B_PIN, PIN_HIGH);
}

// 控制单通道LED（亮：低电平；灭：高电平）
void led_set(uint32_t pin, rt_bool_t on)
{
    rt_pin_write(pin, on ? PIN_LOW : PIN_HIGH);
}

// 演示：RGB循环闪烁
int main(void)
{
    led_init();
    rt_kprintf("RGB LED Demo Start!\n");

    while (1)
    {
        // 红灯亮
        led_set(LED_R_PIN, RT_TRUE);
        rt_thread_mdelay(500);
        led_set(LED_R_PIN, RT_FALSE);
        
        // 绿灯亮
        led_set(LED_G_PIN, RT_TRUE);
        rt_thread_mdelay(500);
        led_set(LED_G_PIN, RT_FALSE);
        
        // 蓝灯亮
        led_set(LED_B_PIN, RT_TRUE);
        rt_thread_mdelay(500);
        led_set(LED_B_PIN, RT_FALSE);
        
        // 全亮（白）
        led_set(LED_R_PIN, RT_TRUE);
        led_set(LED_G_PIN, RT_TRUE);
        led_set(LED_B_PIN, RT_TRUE);
        rt_thread_mdelay(500);
        led_set(LED_R_PIN, RT_FALSE);
        led_set(LED_G_PIN, RT_FALSE);
        led_set(LED_B_PIN, RT_FALSE);
    }

    return 0;
}